﻿var timer = {
    minutes :0,
    seconds : 0,
    elm :null,
    samay : null,
    sep : ':',
    init : function(m,s,elm)
    {
        m = parseInt(m,10);
        s = parseInt(s,10);
        if(m < 0 || s <0 || isNaN(m) || isNaN(s)) { alert('Invalid Values'); return; }
        this.minutes = m;
        this.seconds = s;
        this.elm = document.getElementById(elm);
        timer.start();
    },
    start : function()
    {
        this.samay = setInterval((this.doCountDown),1000);
    },
    doCountDown : function()
    {
        if(timer.seconds == 0)
        {
            if(timer.minutes == 0)
            {
                clearInterval(timer.samay);
                timerComplete();
                return;
            }
            else
            {
                timer.seconds=60;
                timer.minutes--;
            }
        }
        timer.seconds--;
        timer.updateTimer(timer.minutes,timer.seconds);
    },
    updateTimer :  function(min,secs)
    {
        min = (min < 10 ? '0'+min : min);
        secs = (secs < 10 ? '0'+secs : secs);
        (this.elm).innerHTML = min+(this.sep)+secs;
    }
}
function timerComplete()
{
    alert('time out buddy!!!');
}
/* Example
window.onload = init;
function init()
{
    timer.init(0,55,'container');
}*/

// Add Event function
  function addEvent(elm, evType, fn, useCapture)
  {
  	// cross browser event handling for IE 5+, NS6+ and Gecko
  	if (elm.addEventListener)
  	{
  		elm.addEventListener(evType, fn, useCapture);
  		return true;
  	}
  	else if (elm.attachEvent)
  	{
  		var r = elm.attachEvent('on' + evType, fn);
  		return r;
  	}
  	else
  	{
  		elm['on' + evType] = fn;
  	}
  }
    // Add Listeners
  function addListeners(e)
  {
  	// window unload listener
  	addEvent(window, 'unload', exitAlert, false);
  }
  
  // Confirm exit needed
  function exitAlert()
  {
    if(confirm('Are you sure you want to leave the current page?')) {
      __doPostBack('<%=btnStart.UniqueID %>','');
    } else {
      if(window.event) {
        window.event.returnValue = false;
      } else {
        e.preventDefault();
      }
      return false;
    }
  }
  
  // Init
  addEvent(window, 'load', addListeners, false);
  
  